package com.zjuee.base.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author wu liang
 * @since 1.0
 */
public final class MD5Utils {

    private static Logger logger = LoggerFactory.getLogger(MD5Utils.class);
    public static final String ALGORITHM_MD5 = "MD5";

    /**
     * MD5 ENCODE
     *
     * @param content
     * @return base64ToString
     */
    public static String encodeBase64(String content) {

        if (StringUtils.isEmpty(content)) {
            return null;
        }

        try {
            return Base64Utils.encodeToString(MessageDigest.getInstance(ALGORITHM_MD5).digest(content.getBytes(StandardCharsets.UTF_8)));
        } catch (NoSuchAlgorithmException e) {
            logger.error("", e);
        }
        return null;
    }

}
